package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Connectivity.ConnectionManager;

public class EmpRegister extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -342534531098283958L;

	/**
	 * Constructor of the object.
	 */
	public EmpRegister() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}
	
		
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException 
  {
try
  {Connection con=ConnectionManager.getConnection();
	//http://localhost:9090/Genius/EmpRegister?loginid=&password=&confirmpassword=&fname=&mname=&lname=&mothername=&religion=
	//&caste=&subCaste=&bgroup=&gender=M&bdate=&tempAddress=&perAddress=&area=&pcode=&country=%2B91&state=020&phnumber=
	//&country=%2B91&state=020&phnumber1=&isd=%2B91&cellnumber=&marital_status=0&identification=&emailid=&qualification=
	//&experience=&jdate=&dlicense=&pcard=&iname=&iname=&iname=&roleId=0
		String loginid = request.getParameter("loginid")!=null?request.getParameter("loginid"):"";
		String password = request.getParameter("password")!=null?request.getParameter("password"):"";
		String firstname = request.getParameter("fname")!=null?request.getParameter("fname"):"";
		String middlename = request.getParameter("mname")!=null?request.getParameter("mname"):"";
		String lastname = request.getParameter("lname")!=null?request.getParameter("lname"):"";
		String mothername = request.getParameter("mothername")!=null?request.getParameter("mothername"):"";
		String religion = request.getParameter("religion")!=null?request.getParameter("religion"):"";
		String caste = request.getParameter("caste")!=null?request.getParameter("caste"):"";
		String subCaste = request.getParameter("subCaste")!=null?request.getParameter("subCaste"):"";
		String bgroup = request.getParameter("bgroup")!=null?request.getParameter("bgroup"):"";
		String gender = request.getParameter("gender")!=null?request.getParameter("gender"):"";
		String birth_place = request.getParameter("birth_place")!=null?request.getParameter("birth_place"):"PUNE";
		String bdate = request.getParameter("bdate")!=null?request.getParameter("bdate"):"";
		String tempAddress = request.getParameter("tempAddress")!=null?request.getParameter("tempAddress"):"";
		String perAddress = request.getParameter("perAddress")!=null?request.getParameter("perAddress"):"";
		String area = request.getParameter("area")!=null?request.getParameter("area"):"";
		String cellnumber = request.getParameter("cellnumber")!=null?request.getParameter("cellnumber"):"";
		String marital_status = request.getParameter("marital_status")!=null?request.getParameter("marital_status"):"2";
		String identification = request.getParameter("identification")!=null?request.getParameter("identification"):"";
		String emailid = request.getParameter("emailid")!=null?request.getParameter("emailid"):"";
		String qualification = request.getParameter("qualification")!=null?request.getParameter("qualification"):"";
		String experience = request.getParameter("experience")!=null?request.getParameter("experience"):"";
		String jdate = request.getParameter("jdate")!=null?request.getParameter("jdate"):"";
		String dlicense = request.getParameter("dlicense")!=null?request.getParameter("dlicense"):"2";
		String pcard = request.getParameter("pcard")!=null?request.getParameter("pcard"):"";
		String image_name = request.getParameter("image_name")!=null?request.getParameter("image_name"):"spacer.gif";
		String roleid = request.getParameter("roleId")!=null?request.getParameter("roleId"):"";

if(jdate.equals(""))
{
	jdate="2000-04-19";

}
else
{
	
	String dates[] = jdate.split("-");
	
	
String year=dates[2];

System.out.println("==222=============="+year);		

String month=dates[1];
System.out.println("=====333==========="+month);		

String date=dates[0];
jdate=year+"-"+month+"-"+date;

System.out.println(jdate);

}
		
String empquerry="Insert into employee_master(loginid,emp_id,password,name,father_name,sex,birth_place,mobile_no,date_of_joining,status,role_id,birth_date,last_name,image_name)";
empquerry=empquerry+" values(?,nextval('employee_master_seq'),?,?,?,?,?,?,to_date('"+jdate+"','yyyy-mm-dd')";
empquerry=empquerry+",'V',?,?,?,?)";
	PreparedStatement empprs=con.prepareStatement(empquerry);
	empprs.setString(1,loginid);
	empprs.setString(2,password);
	empprs.setString(3,firstname);
	empprs.setString(4,middlename);
	empprs.setString(5,gender);
	empprs.setString(6,birth_place);
	empprs.setString(7,cellnumber);
	empprs.setInt(8,Integer.parseInt(roleid));
	empprs.setString(9,bdate);
	empprs.setString(10,lastname);
	empprs.setString(11,image_name);
	boolean k=empprs.execute();
	System.out.println("image_name=:"+image_name);
	System.out.println("k=:"+k);
		if (k==false)
		{response.sendRedirect("/Genius/form/employee/EmpRegistration.jsp?status=I");}
		
		
		
  }
catch (Exception e)
{e.printStackTrace();}
	
		
  
  }

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
